<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "bus_service_extend".
 *
 * @property string $extend_id
 * @property string $service_id
 * @property string $beautycartd_id
 * @property string $beautycartd_name
 * @property string $employee_id
 * @property string $employee_name
 * @property string $consume_amount
 * @property double $reward
 * @property integer $is_reward
 */
class BusServiceExtend extends \yii\db\ActiveRecord
{
    public $errorMessage;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bus_service_extend';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['service_id', 'beautycartd_id', 'employee_id'], 'required'],
            [['service_id', 'beautycartd_id', 'employee_id', 'is_reward'], 'integer'],
            [['reward', 'consume_amount'], 'number'],
            [['beautycartd_name', 'employee_name', 'add_time'], 'string', 'max' => 50]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'extend_id' => 'Extend ID',
            'service_id' => '服务记录id',
            'member_id' => '会员id',
            'beautycartd_id' => '卡项id',
            'beautycartd_name' => '卡项名称',
            'employee_id' => '员工id',
            'employee_name' => '员工名称',
            'consume_amount' => '疗程卡使用一次的金额',
            'reward' => '员工的服务提成',
            'is_reward' => '是否记录提成',
            'add_time' => '记录时间',
            'shop_id' => '店铺id',
            'boss_id' => 'boss_id',
        ];
    }

    public function checkParams()
    {
        if (!$this->service_id) {
            $this->errorMessage = '服务记录id错误';
            return false;
        }
        if (empty($this->beautycartd_id)) {
            $this->errorMessage = '卡项id不能为空';
            return false;
        }
        if (empty($this->beautycartd_name)) {
            $this->errorMessage = '卡项名称不能为空';
            return false;
        }
        if (empty($this->employee_id)) {
            $this->errorMessage = '员工id不能为空';
            return false;
        }
        if (empty($this->employee_name)) {
            $this->errorMessage = '员工名称不能为空';
            return false;
        }
        if (!is_numeric($this->reward) || $this->reward < 0) {
            $this->errorMessage = '员工的服务提成参数错误';
            return false;
        }

        return true;
    }

    public function addData($is_check = true)
    {
        if ($is_check && !$this->checkParams()) {
            return false;
        }

        $BusMemberBeautycard = BusMemberBeautycard::findOne($this->beautycartd_id);
        $BusEmployee = BusEmployee::findOne($this->employee_id);

        $this->beautycartd_name = $BusMemberBeautycard->beautycartd_name;
        $this->employee_name = $BusEmployee->employee_name;

        if ($this->insert(false)) {
            return true;
        } else {
            $this->errorMessage = '添加失败';
            return false;
        }
    }
}
